<?php
declare (strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Cache;
use think\facade\Db;

require_once './sdk/WxpayAPI/lib/WxSend.Api.php';

/**提醒骑手送货
 * Class AutomaticDelivery
 * @package app\command
 */
class AutomaticRemindRider extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('AutomaticRemindRider')
            ->setDescription('the AutomaticRemindRider command');
    }

    protected function execute(Input $input, Output $output)
    {
        try {
            $redis = Cache::store('redis')->handler();
            $sends = $redis->rPop('send');//从redis中取出来
            if (!empty($sends)) {
                $send_ids = explode(',', $sends);
                // 自动收货(系统评价)
                $page = 1;
                $limit = 50;
                $send = new \WxSendApi();
                while (true) {
                    $ids = Db::table('eb_user')->where('id', 'in', $send_ids)->field('openid')->page($page, $limit)->select()->toArray();
                    file_put_contents('/tmp/rider.txt', json_encode($ids), FILE_APPEND);
                    if (!$ids) break;
                    $openid_ids = array_column($ids, 'openid');
//                    file_put_contents('/tmp/a.txt', json_encode($openid_ids), FILE_APPEND);
                    foreach ($openid_ids as $key => $value) {
//                    file_put_contents('/tmp/c.txt', json_encode($value['openid']), FILE_APPEND);
                    $send = new \WxSendApi();
                    $dd = $send->send_Template_rider($value);
                    file_put_contents('/tmp/rider.txt', json_encode($dd), FILE_APPEND);
                    }
                    $page++;
                }
            }
        } catch (\Exception $e) {
            exit;
        }
    }
}
